from django.http import JsonResponse


def login_required_decorator(func):
    # 判断用户是否登录
    def wrapper(request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({"errno": "4101", "errmsg": "用户未登录"})
        return func(request, *args, **kwargs)

    return wrapper


class LoginRequired:
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        return login_required_decorator(view)
